<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{obj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="obj.aut_name" :label="formatTime(obj.pubdate)">
        <template #icon>
          <img :src="obj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="obj.is_followed"
            @click="followedFn(false)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain
            @click="followedFn(true)"
              v-else >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="obj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box" id="like-box">
        <van-button icon="good-job" type="danger" size="small"
          v-if="obj.attitude === 1" @click="attiFn(-1)">已点赞</van-button
        >
        <van-button icon="good-job-o" type="danger" plain size="small" @click="attiFn(1)"
          v-else >点赞</van-button
        >
      </div>
    </div>
    <!-- 评论区域 -->
   <Comment v-if="artId" :artId='artId'></Comment>
  </div>
</template>

<script>
import {
  getArticleDetailAPI, followAuthorAPI,
  unfollowAuthorAPI, likeArticleAPI,
  unlikeArticleAPI
} from '@/api/article_detail'

import { Toast } from 'vant'
import Comment from '@/views/ArticleDetail/CommentList.vue'
export default {
  data () {
    return {
      obj: {},
      comments: [],
      list: {}
    }
  },
  components: {
    Comment
  },
  computed: {
    artId () {
      return this.obj.art_id
    }
  },
  methods: {
    // 关注 与 取消关注
    async followedFn (bool) {
      this.obj.is_followed = bool
      if (bool === true) {
        try {
          await followAuthorAPI({ target: this.obj.aut_id })
          Toast('关注成功')
        } catch (err) {
          Toast('关注失败')
        }
      } else {
        try {
          await unfollowAuthorAPI({ id: this.obj.aut_id })
          Toast('取消关注')
        } catch (err) {
          Toast('取消关注失败')
        }
      }
    },
    // 点赞 与 取消点赞
    async attiFn (num) {
      this.obj.attitude = num
      if (num === 1) {
        try {
          await likeArticleAPI({ target: this.obj.art_id })
          Toast('已点赞')
        } catch (err) {
          Toast('点赞失败')
        }
      } else {
        try {
          await unlikeArticleAPI({ id: this.obj.art_id })
          Toast('已取消')
        } catch (err) {
          Toast('取消点赞失败')
        }
      }
    }
  },
  async created () {
    const res = await getArticleDetailAPI({ id: this.$route.query.aid })
    this.obj = res.data.data
    // console.log(this.obj)
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
